<template>
	<Layout :showFooter="true">
		<template #title>{{action === Action.Add ?'新增':'编辑'}}荣誉</template>

		<SectionTitle showLine :showRight="false"><template #title>荣誉标题</template></SectionTitle>
		<InfoBlock standard customStyle="margin-top:18rpx;">
			<uni-easyinput type="textarea" :inputBorder="false" v-model="formData.title"
				placeholder="请输入内容" maxlength="100"></uni-easyinput>
			<view class="iflex fill jcfe" style="font-size: 28rpx; color:#909090;">{{formData.title.length}}/100</view>
		</InfoBlock>


		<SectionTitle showLine :showRight="false"><template #title>封面图</template></SectionTitle>
		<view class="iflex fill" style="padding:0 32rpx;">
			<UploadImage v-model="formData.awardImage" :limit="1"></UploadImage>
		</view>

		<SectionTitle showLine><template #title>荣誉内容</template></SectionTitle>
		<InfoBlock standard customStyle="margin-top:18rpx;">
			<uni-easyinput type="textarea" :inputBorder="false" v-model="formData.awardContent"
				placeholder="请输入内容" maxlength="500"></uni-easyinput>
			<view class="iflex fill jcfe" style="font-size: 28rpx; color:#909090;">{{formData.awardContent.length}}/500
			</view>
		</InfoBlock>


		<template #footer>
			<view class="common-footer">
				<u-button v-if="action === Action.Edit"
					:customStyle="{width:'220rpx',border:'none',backgroundColor:'#F6F6F6',color:'#757575'}"
					:hair-line="false" size="large" @click="tapDelete">删除</u-button>
				<u-button :customStyle="{width:action === Action.Edit ?'430rpx':'686rpx'}" type="primary" size="large"
					@click="tapSubmit">提交</u-button>
			</view>
		</template>
	</Layout>
	<u-top-tips ref="tipsRef" :navbar-height="boundingRect.safeArea.height"></u-top-tips>
</template>

<script setup lang="ts" name="LawyerEditFile">
	import { ref, inject } from 'vue';
	import { useShowTipMessage } from '@/hooks/util';
	import { getHonor, saveHonor, deleteHonor } from '@/apis/lawfirm';
	import { onLoad, onPullDownRefresh } from '@dcloudio/uni-app';
	import type { AnyObj } from '@/types/global';
	import { isEmpty, backRefresh } from '@/utils/util';
	import { isResultSuccess } from '@/utils/http';
	import { Action } from '@/utils/constant';
	import type { BoundingRect } from '@/hooks/bounding-rect';
	const boundingRect = inject<BoundingRect>('boundingRect');
	const [tipsRef, showTipMessage] = useShowTipMessage();

	const action = ref<Action>(Action.Empty);
	const formData = ref<AnyObj>({
		awardId: '',
		lawFirmId: '',
		title: '',
		awardImage: '',
		awardContent: '',
	})


	function tapDelete() {
		uni.showModal({
			content: '确定要删除吗？',
			confirmColor: '#FF710A',
			success: function (res) {
				if (!res.confirm) return;
				deleteHonor(formData.value.awardId).then((res) => {
					if (!isResultSuccess(res, '删除成功')) return;
					backRefresh('law-honor-refresh');
				});
			}
		});
	}
	function tapSubmit() {
		const value = formData.value;
		if (isEmpty(value.title)) {
			showTipMessage('请输入标题，长度 100 个字符以内', 'error');
			return;
		}

		if (value.title.length > 100) {
			showTipMessage('标题长度 100 个字符以内', 'error');
			return;
		}

		if (isEmpty(value.awardImage)) {
			showTipMessage('请上传封面图', 'error');
			return;
		}

		if (isEmpty(value.awardContent)) {
			showTipMessage('请输入荣誉内容，长度 500 个字符以内', 'error');
			return;
		}

		if (value.awardContent.length > 500) {
			showTipMessage('荣誉内容长度 500 个字符以内', 'error');
			return;
		}

		saveHonor({ ...value }).then((res) => {
			if (!isResultSuccess(res)) return;
			backRefresh('law-honor-refresh');
			// // #ifdef MP-WEIXIN
			// const pages = getCurrentPages();
			// console.log('pages', pages.at(-1));
			// // #endif
		});
	}

	onLoad((options) => {
		options = options as { lawFirmId : string; awardId : string; action : Action }
		formData.value.lawFirmId = options.lawFirmId;
		formData.value.awardId = options.awardId;
		action.value = options.action ?? Action.Add;
		console.log(action.value === Action.Edit);
		if (action.value === Action.Edit) {
			getHonor(options.awardId).then((res) => {
				formData.value = res.data;
			});
		}
	});
</script>

<style lang="scss">

</style>